package com.frame.pageform;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

public class Iterate {

	private static class ArrayIterator implements Iterator
	{
		private Object array;

		private int length;

		private int index;

		private ArrayIterator(Object array)
		{
			this.array = array;
			length = Array.getLength(array);
		}

		public boolean hasNext()
		{
			return index < length;
		}

		public Object next()
		{
			return Array.get(array, index++);
		}

		public void remove()
		{
			throw new UnsupportedOperationException("此方法不能使用");
		}
	}

	private Iterate()
	{
	}

	static public Iterator getIterator(Object obj)
	{
		if (obj instanceof Iterator)
			return (Iterator) obj;
		else if (obj instanceof Enumeration)
			return Collections.list( (Enumeration) obj).iterator();
		else if (obj instanceof Collection)
			return ( (Collection) obj).iterator();
		else if (obj instanceof Map)
			return ( (Map) obj).entrySet().iterator();
		else if (obj.getClass().isArray())
			return new ArrayIterator(obj);

		return null;
	}
}
